#include "StdAfx.h"
#include "NumberEdit.h"

/////////////////////////////////////////////////////////////////////////////
// CNumEdit

CNumEdit::CNumEdit()
{
    m_bret=FALSE;
}

CNumEdit::~CNumEdit()
{

}


BEGIN_MESSAGE_MAP(CNumEdit, CEdit)
    //{{AFX_MSG_MAP(CNumEdit)
    ON_WM_CHAR()
    ON_CONTROL_REFLECT(EN_KILLFOCUS, OnKillfocus)
    ON_WM_DESTROY()
    ON_WM_LBUTTONDOWN()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNumEdit message handlers

void CNumEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
    // TODO: Add your message handler code here and/or call default
    if ((nChar>='0' && nChar<='9') 
        || nChar==0x8 
        || nChar==0x3 
        || nChar==0x16 
        || nChar==0x18
        || nChar==0x26
        || nChar=='.'
        || nChar=='-')
    {

        if (nChar=='.')
        {
            CString str;
            this->GetWindowText(str);
            DWORD dw=this->GetSel();
            if (str.Find('.',0)!=-1)
            {
                if (str.Mid(LOWORD(dw),HIWORD(dw)-LOWORD(dw)).Find('.',0)==-1)
                {
                    return;
                }
            }

        }
        else if(nChar == '-')
        {
            CString str;
            this->GetWindowText(str);
            if (str.GetLength() > 0)
            {
                if (str.GetAt(0) != '-')
                {
                    str.Insert(0, '-');
                    int n1, n2;
                    this->GetSel(n1, n2);
                    this->SetWindowText(str);
                    this->SetSel(n1+1, n2+1);
                }

                return;
            }
        }

        CEdit::OnChar(nChar, nRepCnt, nFlags);	
    }
}

void CNumEdit::OnKillfocus() 
{
    // TODO: Add your control notification handler code here
    CString str;
    int count=0;
    GetWindowText(str);
    if (str.Trim() == "" || str.Trim() == "-" || str.Trim() == ".")
    {
        SetWindowText("0");
    }

    if (m_bret)
    {
        return;
    }

    for (int i=0;i<str.GetLength();i++)
    {
        CHAR a=str.GetAt(i);

        if (i==0 && a == '-')
            continue;

        if ((str.GetAt(i)>='0' && str.GetAt(i)<='9')||str.GetAt(i)=='.')
        {
            if (str.GetAt(i)=='.')
            {
                if (count++>=2)
                {
                    SetFocus();
                    SetSel(0,-1);
                    return;
                }
            }
        }
        else
        {
            SetFocus();
            SetSel(0,-1);
            return;
        }
    }
}

BOOL CNumEdit::PreTranslateMessage(MSG* pMsg) 
{
    // TODO: Add your specialized code here and/or call the base class
    if (pMsg->message==WM_KEYDOWN||pMsg->message==WM_KEYUP)
    {
        if (pMsg->wParam==VK_ESCAPE)
        {
            m_bret=TRUE;
        }
    }
    return CEdit::PreTranslateMessage(pMsg);
}



void CNumEdit::OnDestroy() 
{

    CEdit::OnDestroy();
}

LRESULT CNumEdit::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
    // TODO: Add your specialized code here and/or call the base class
    if (message==WM_DESTROY)
    {
        m_bret=TRUE;
    }
    return CEdit::WindowProc(message, wParam, lParam);
}

void CNumEdit::OnLButtonDown(UINT nFlags, CPoint point) 
{
    CEdit::OnLButtonDown(nFlags, point);
}



